﻿@page "/materialcategories/index"
@attribute [Authorize]


@inject ExampleJsInterop JsInterop
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject IAuthorizationService AuthorizationService
@inject DialogService DialogService
@inject IMaterialCategoryService MaterialCategoryService

<RadzenCard Variant="Variant.Filled" Style="height : 100%;">
    <RadzenSplitter Orientation="Orientation.Vertical" style="height:100%;">
        <RadzenSplitterPane Size="100px">
            <RadzenSplitter>
                <RadzenSplitterPane Size="20%" Min="20%" Max="30%">
                    <RadzenTree Data=@TreeData Change=@ChangeHandler @bind-Value=_selectionTreeData Style="height: 100%;width:100%">
                        <RadzenTreeLevel TextProperty="Name" ChildrenProperty="Children"
                                         HasChildren=@(e => (e as MaterialCategoryTreeResponseDto).Children?.Any()??false) />
                    </RadzenTree>
                </RadzenSplitterPane>
                <RadzenSplitterPane>
                    <RadzenStack class="rz-m-2">

                        <SearchTablePartial @ref="_requestTable" T="MaterialCategoryInfoResponseDto" ServerData="ServerReload"
                                            SearchPartial="_searchPartial" AddHandler="AddHandler"
                                            EditHandler="EditHandler"
                                            ExportExcelHandler="DownloadExcelHandler">
                        </SearchTablePartial>
                    </RadzenStack>
                </RadzenSplitterPane>
            </RadzenSplitter>
        </RadzenSplitterPane>
    </RadzenSplitter>
</RadzenCard>
@code {

    private IEnumerable<MaterialCategoryInfoResponseDto> _pagedData = new List<MaterialCategoryInfoResponseDto>();
    private SearchPartialInfo _searchPartial = new();
    private SearchTablePartial<MaterialCategoryInfoResponseDto> _requestTable = new();
    private int _totalItems;
    private MaterialCategorySearchRequestDto _searchRequestDto = new();

    // protected MaterialCategoryEditRequestDto EditRequestDto = new();
    // protected MaterialCategoryEdit EditComponent;

    protected List<MaterialCategoryTreeResponseDto> TreeData = new();
    private object _selectionTreeData = null;


    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        // var authenticationState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
        _searchPartial.IsAdd = true;
        _searchPartial.IsEdit = true;
        _searchPartial.IsDelete = true;
        _searchPartial.IsImportExcel = true;

        await LoadTreeServerData();
    }

    async Task LoadTreeServerData()
    {
        var result = await MaterialCategoryService.QueryMaterialCategoriesByTree();
        if (result.Succeeded)
        {
            TreeData = result.Data;
        }
    }

    async Task AddHandler(MouseEventArgs args)
    {
        await DialogService.OpenAsync<MaterialCategoryEdit>("Add MaterialCategory", null);
        await _requestTable.ReloadServerData();
    }

    async Task EditHandler(MouseEventArgs args, MaterialCategoryInfoResponseDto context)
    {
        await DialogService.OpenAsync<MaterialCategoryEdit>("Edit MaterialCategory", new Dictionary<string, object> { { "Key", context.Id } });
        await _requestTable.ReloadServerData();
    }

    async Task ChangeHandler()
    {
        await _requestTable.ReloadServerData();
    }

    Task DownloadExcelHandler()
    {
        // var response = await MaterialCategoryService.ExportMaterialCategorys();
        // var fileName = HttpResponseMessageHelper.GetFileName(response);
        // var stream = await response.Content.ReadAsStreamAsync();
        // await JsInterop.DownloadFileFromStream(stream, fileName);
        return Task.CompletedTask;
    }

    Task ImportExcelHandler()
    {
        // var dialog = await DialogService.ShowAsync<MaterialCategoryImport>("导入", _dialogOptions);
        return Task.CompletedTask;
    }

    /// <summary>
    /// Here we simulate getting the paged, filtered and ordered data from the server
    /// </summary>
    private async Task<ODataServiceResult<MaterialCategoryInfoResponseDto>> ServerReload(LoadDataArgs state)
    {
        _searchRequestDto = new MaterialCategorySearchRequestDto();

        if (_selectionTreeData != null)
        {
            var materialCategoryTreeResponseDto = (MaterialCategoryTreeResponseDto)_selectionTreeData;
            _searchRequestDto.ParentId = materialCategoryTreeResponseDto.Key;
        }

        if (state.Skip != null) _searchRequestDto.Page = (int)(state.Skip + 1);
        if (state.Top != null) _searchRequestDto.Limit = (int)state.Top;
        var data = await MaterialCategoryService.QueryMaterialCategories(_searchRequestDto);

        if (data != null)
        {
            _pagedData = data.Data;
            _totalItems = data.TotalCount;
        }

        return new ODataServiceResult<MaterialCategoryInfoResponseDto>() { Count = _totalItems, Value = _pagedData };
    }

}